package com.gxuwz.medical.tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.user.User;


public class UserDao {
	public  List<User> query() throws SQLException{
		Connection con ;
		PreparedStatement stmd = null;
		ResultSet rs = null;

		String sql = "select * from t_user";
		con = DbUtil.getConn();
		stmd = con.prepareStatement(sql);
		rs = stmd.executeQuery();
		List<User> lis = new ArrayList<User>();
		User u =null;
		while(rs.next()){
			u = new User();
	    	u.setUserid(rs.getString(1));
	    	u.setPwd(rs.getString(2));
	    	u.setFullname(rs.getString(3));
	    	lis.add(u);
		}
		
		rs.close();
		stmd.close();
		con.close();
		return lis;
		
	}
	public boolean save(User u){
		Connection con=null;
		PreparedStatement stmd = null;
		String sql="insert into t_user values(?,?,?)";
		boolean flag=false;
			con=DbUtil.getConn();
			try {
				stmd=con.prepareStatement(sql);
				stmd.setString(1, u.getUserid());
				stmd.setString(2, u.getPwd());
				stmd.setString(3, u.getFullname());
				flag=stmd.executeUpdate()>0;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return flag;
	}
	public boolean update(User u){
		Connection con=null;
		PreparedStatement stmd = null;
		String sql="update t_user set pwd=?,fullname=? where userid=?";
		boolean flag=false;
			con=DbUtil.getConn();
			try {
				stmd=con.prepareStatement(sql);
				
				stmd.setString(1, u.getPwd());
				stmd.setString(2, u.getFullname());
				stmd.setString(3, u.getUserid());
				flag=stmd.executeUpdate()>0;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return flag;
	}
	public boolean delete(String userid){
		Connection con=null;
		PreparedStatement stmd = null;
		String sql="delete from t_user where userid=?";
		boolean flag=false;
			con=DbUtil.getConn();
			try {
				stmd=con.prepareStatement(sql);
				
				stmd.setString(1, userid);
				flag=stmd.executeUpdate()>0;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return flag;
	}
	public static void main(String[] args) throws SQLException {
		UserDao u = new UserDao();
		//List ls = null;
		User uu = new User();
		uu.setUserid("333");
		uu.setPwd("4445");
		uu.setFullname("白白5");
		//u.update(uu);
		//u.delete("345345");
		System.out.println(uu);
	}
}
